package com.niuniu.rushdorm.controller;

import com.niuniu.rushdorm.pojo.entity.Student;
import com.niuniu.rushdorm.pojo.vo.UserVO;
import com.niuniu.rushdorm.pojo.vo.ResultVO;
import com.niuniu.rushdorm.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    OrderService orderService;

    @PostMapping("/create")
    public ResultVO createOrder(@RequestBody Map<String, Integer> requestMap, HttpServletRequest request) {

        Integer buildingId = requestMap.get("building_id");

        //设置日志内容
        String logContent = "buildingId:"+buildingId;
        request.setAttribute("log_content",logContent);

        UserVO userVO = (UserVO) request.getAttribute("userVO");
        Integer classLimit = (Integer) request.getAttribute("class_limit");
        //不限制班级，设为null
        if(classLimit!=null && classLimit.equals(0)){
            Student student = userVO.getStudent();
            student.setClassId(null);
            userVO.setStudent(student);
        }
        Integer orderId = orderService.createOrder(userVO, buildingId);

        return ResultVO.success(orderId);
    }
}
